﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Catering_DBOpertion;

namespace Catering_Operation
{
    public class Service_Unit
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int sid)
        {
            string strSql = "select count(1) from Service_Unit where SID=@SID ";
            SqlParameter[] parameters = { new SqlParameter("@SID", SqlDbType.Int, 4) };
            parameters[0].Value = sid;
            return DbHelperSQL.Exists(strSql, parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.Service_Unit model)
        {
            string strSql = "insert into Service_Unit(SName,SBack) values (@SName,@SBack)";
            SqlParameter[] parameters = {
					new SqlParameter("@SName", SqlDbType.NVarChar,50),
					new SqlParameter("@SBack", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.SName;
            parameters[1].Value = model.SBack;

            DbHelperSQL.ExecuteSql(strSql, parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.Service_Unit model)
        {
            string strSql = "update Service_Unit set SName=@SName,SBack=@SBack where SID=@SID ";
            SqlParameter[] parameters = {
					new SqlParameter("@SName", SqlDbType.NVarChar,50),
					new SqlParameter("@SBack", SqlDbType.NVarChar,50),
					new SqlParameter("@SID", SqlDbType.Int,4)};
            parameters[0].Value = model.SName;
            parameters[1].Value = model.SBack;
            parameters[2].Value = model.SID;

            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int sid)
        {

            string strSql = "delete from Service_Unit where SID=@SID ";
            SqlParameter[] parameters = {
					new SqlParameter("@SID", SqlDbType.Int,4)};
            parameters[0].Value = sid;

            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.Service_Unit GetModel(int sid)
        {

            string strSql = "select  top 1 SID,SName,SBack from Service_Unit where SID=@SID ";
            SqlParameter[] parameters = { new SqlParameter("@SID", SqlDbType.Int, 4) };
            parameters[0].Value = sid;
            Catering_Model.Service_Unit model = new Catering_Model.Service_Unit();
            DataSet ds = DbHelperSQL.Query(strSql, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (!(ds.Tables[0].Rows[0]["SID"] is DBNull))
                {
                    model.SID = int.Parse(ds.Tables[0].Rows[0]["SID"].ToString());
                }
                if (!(ds.Tables[0].Rows[0]["SName"] is DBNull) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["SName"].ToString()))
                {
                    model.SName = ds.Tables[0].Rows[0]["SName"].ToString();
                }
                if (!(ds.Tables[0].Rows[0]["SBack"] is DBNull) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["SBack"].ToString()))
                {
                    model.SBack = ds.Tables[0].Rows[0]["SBack"].ToString();
                }
                ds.Tables[0].Dispose();
                ds.Dispose();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            string strSql = "select SID,SName,SBack FROM Service_Unit ";
            if (strWhere.Trim() != "") strSql += " where " + strWhere;
            return DbHelperSQL.Query(strSql);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) strSql.Append(" top " + Top.ToString());
            strSql.Append(" SID,SName,SBack ");
            strSql.Append(" FROM Service_Unit ");
            if (strWhere.Trim() != "") strSql.Append(" where " + strWhere);
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
